import 'package:dio/dio.dart';
import 'dart:convert';
import 'package:flutter/material.dart';

class CategoryItem extends StatefulWidget {
  CategoryItem(
      {Key key,
      this.id,
      this.name,
      this.cover,
      this.desc,
      this.create_time,
      this.favorite_count})
      : super(key: key);

  int id;
  final String name;
  final String desc;
  final String cover;
  final String create_time;
  int favorite_count;
  @override
  CategoryItemWState createState() => new CategoryItemWState(this.id, this.name,
      this.cover, this.desc, this.create_time, this.favorite_count);
}

class CategoryItemWState extends State<CategoryItem> {
  int id;
  String name;
  String desc;
  String cover;
  String create_time;
  int favorite_count;
  CategoryItemWState(this.id, this.name, this.cover, this.desc,
      this.create_time, this.favorite_count);

  Dio dio = Dio();
  bool isFavorite = false;
  Color fontColor = Color.fromRGBO(255, 75, 54, 1);
  Color fontUnselectColor = Color.fromRGBO(200, 200, 200, 1);
  _onSelectedIndex() async {
    if (isFavorite) {
      final String url = 'http://127.0.0.1:7001/favorite/cancel';
      final res = await dio.post(url,
          data: {'user_id': 1, 'favorite_type': 1, 'cook_id': this.id});
      isFavorite = false;
      this.favorite_count -= 1;
    } else {
      final String url = 'http://127.0.0.1:7001/cook/setFavoriteCount';
      final res = await dio
          .post(url, data: {'user_id': 1, 'favorite_type': 1, 'id': this.id});
      isFavorite = true;
      this.favorite_count += 1;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    double mediaWidth = MediaQuery.of(context).size.width; // 屏幕宽度
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
          border: Border(bottom: BorderSide(width: 1, color: Colors.black12))),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              ClipOval(
                child: Image(
                  image: AssetImage("images/2.jpg"),
                  fit: BoxFit.cover,
                  width: 35,
                  height: 35,
                ),
              ),
              Padding(
                padding: const EdgeInsets.fromLTRB(15, 0, 0, 0),
                child: Text("小飘飘"),
              ),
              Expanded(child: Text(create_time, textAlign: TextAlign.right))
            ],
          ),
          Container(
              padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(10.0))),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(10),
                child: Image(
                    image: NetworkImage(cover),
                    fit: BoxFit.cover,
                    width: mediaWidth - 40),
              )),
          Row(
            children: [
              IconButton(
                icon: Icon(isFavorite == true
                    ? Icons.favorite
                    : Icons.favorite_border),
                color: isFavorite == true ? fontColor : fontUnselectColor,
                onPressed: () {
                  _onSelectedIndex();
                },
              ),
              Padding(
                padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                child: Text("点赞:${favorite_count}"),
              )
            ],
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
          ),
          Text(
            name,
            style: TextStyle(
              fontWeight: FontWeight.w500,
              fontSize: 20.0,
              height: 1.5,
              fontFamily: "Courier",
              color: Colors.black,
            ),
          ),
          Text(
            desc,
            style: TextStyle(
              fontSize: 16.0,
              height: 1.5,
              fontFamily: "Courier",
              color: Colors.black87,
            ),
          )
        ],
      ),
    );
  }
}
